﻿using UnityEngine;
using System;
using System.Linq;
using System.Collections;

namespace Hont.C2UTool
{
    public class C2UGeneralScriptExporter : IC2UScriptExporter
    {
        public int Priority { get { return 1; } }

        public bool CanSlover(Component component)
        {
            return true;
        }

        public string Execute(Component component)
        {
            var publicFields = C2UUtility.GetObjectPublicFields(component);
            var titleStr = component.GetType().FullName + "*";
            var contentStr = "";


            for (int i = 0; i < publicFields.Length; i++)
            {
                var item = publicFields[i];
                var type = "";

                switch (item.FieldType.FullName)
                {
                    case "UnityEngine.Color":
                        type = "color";
                        break;
                    case "UnityEngine.Vector3":
                        type = "vector";
                        break;
                    case "System.Single":
                        type = "float";
                        break;
                    case "System.Boolean":
                        type = "bool";
                        break;
                    case "System.Int32":
                        type = "int";
                        break;
                    case "System.String":
                        type = "string";
                        break;
                    default:
                        type = "Unknow";
                        break;
                }

                if (type != "Unknow")//Complex Type
                {
                    contentStr += ",";

                    var outputName = item.Name;
                    var outputType = type;
                    var outputValue = item.GetValue(component);
                    var outputValueStr = outputValue != null ? outputValue.ToString() : null;

                    if (outputValue is UnityEngine.Color)
                    {
                        var tmpColor = (UnityEngine.Color)outputValue;

                        outputValueStr = string.Format("{0}_{1}_{2}", tmpColor.r, tmpColor.g, tmpColor.b);
                    }

                    else if (outputValue is UnityEngine.Vector3)
                    {
                        var tmpVector3 = (UnityEngine.Vector3)outputValue;

                        outputValueStr = string.Format("{0}_{1}_{2}", tmpVector3.x, tmpVector3.y, tmpVector3.z);
                    }

                    outputValueStr = C2UUtility.ConvertSecurityString(outputValueStr);

                    contentStr += string.Format("Name-{0}#Type-{1}#Value-{2}", outputName, outputType, outputValueStr);
                }
            }

            var attachStr = "";

            if (component is Behaviour)
            {
                attachStr += string.Format("Name-{0}#Type-{1}#Value-{2},", "m_Enabled", "bool", "True");
            }

            var finalStr = titleStr + attachStr + contentStr.TrimStart(',');

            return finalStr;
        }
    }
}
